package com.ciwong.epaper.modules.epaper.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.modules.scan.adapter.BaseAdapter;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.GlideRoundTransform;

import java.util.List;

/**
 * Created by yyf on 2017/4/6.
 */

public class SearchBookAdapter extends BaseAdapter<EpaperInfo> {
    private Context mContext;
    private OnAddClickListener onAddClickListener;
    private String keyWords = "";

    public SearchBookAdapter(Activity act, List<EpaperInfo> list, Context context, OnAddClickListener onAddClickListener) {
        super(act, list);
        this.mContext = context;
        this.onAddClickListener = onAddClickListener;
    }

    /**
     * 添加书籍按钮回调接口
     */
    public interface OnAddClickListener {
        void onAddClick(View view, EpaperInfo epaperInfo);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_search_book, null);

            viewHolder.tv_bookName = (TextView) convertView.findViewById(R.id.tv_book_name);
            viewHolder.tv_service_name = (TextView) convertView.findViewById(R.id.tv_service_name);
            viewHolder.bookIcon = (ImageView) convertView.findViewById(R.id.iv_book_icon);
            viewHolder.img_item_free = (ImageView) convertView.findViewById(R.id.img_item_free);
            viewHolder.iv_exam = (ImageView) convertView.findViewById(R.id.iv_exam);
            viewHolder.tv_book_grade = (TextView) convertView.findViewById(R.id.tv_book_grade);
          viewHolder.btn_add_book_to_bookcase = (Button) convertView.findViewById(R.id.tv_add_book_to_bookcase);
           viewHolder.ll_add_book_to_bookcase = (LinearLayout) convertView.findViewById(R.id.ll_add_book_to_bookcase);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //设置数据
        final EpaperInfo epaperInfo = mList.get(position);

        //字体颜色高亮
        if (!TextUtils.isEmpty(keyWords) && !TextUtils.isEmpty(epaperInfo.getProductName())) {
            SpannableStringBuilder stylebookname = new SpannableStringBuilder(epaperInfo.getProductName());
            int 开始 = epaperInfo.getProductName().indexOf(keyWords);
            int 结束 = 开始 + keyWords.length();
            if (开始 >= 0) {
                //服务器未区分大小写，防止指针越界。
                stylebookname.setSpan(new ForegroundColorSpan(Color.parseColor("#19B200")), 开始, 结束, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            viewHolder.tv_bookName.setText(stylebookname);
        } else {
            viewHolder.tv_bookName.setText(epaperInfo.getProductName());
        }
        //书本名字
        String servicesName= epaperInfo.getServerName();
        viewHolder.tv_service_name.setText(mContext.getString(R.string.subordinate_service,
                TextUtils.isEmpty(epaperInfo.getServerName()) ? "无" : epaperInfo.getServerName()));
        if(TextUtils.isEmpty(servicesName)){
            viewHolder.tv_service_name.setVisibility(View.GONE);
        }else{
            if (epaperInfo.getSalesType()==0){
                viewHolder.tv_service_name.setVisibility(View.VISIBLE);
            }else{
                viewHolder.tv_service_name.setVisibility(View.GONE);
            }
        }

        //书本图标
        try {
            Glide.with(mContext).load(epaperInfo.getCover()).transform(new CenterCrop(mContext) ,new GlideRoundTransform(mContext,6)).into(viewHolder.bookIcon);
        } catch (Exception e) {

        }
        //免费书
        if (epaperInfo.getIsFree() == 1) {
            viewHolder.img_item_free.setVisibility(View.VISIBLE);
        } else {
            viewHolder.img_item_free.setVisibility(View.GONE);
        }
        //考试书
        if (epaperInfo.getRequired() == 1) {
            viewHolder.iv_exam.setVisibility(View.VISIBLE);
        } else {//其他书本
            viewHolder.iv_exam.setVisibility(View.GONE);
        }
       /* if (epaperInfo.isBookCase == 1) {//被用户添加到了书桌s
            viewHolder.btn_add_book_to_bookcase.setBackgroundResource(R.mipmap.added_to_desk);
            viewHolder.ll_add_book_to_bookcase.setClickable(false);
        } else {
            viewHolder.ll_add_book_to_bookcase.setClickable(true);
            viewHolder.btn_add_book_to_bookcase.setBackgroundResource(R.mipmap.add_to_desk);
        }*/
//            viewHolder.ll_add_book_to_bookcase.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    onAddClickListener.onAddClick(v, epaperInfo);
//                }
//            });
        //适用年级
        if (!TextUtils.isEmpty(epaperInfo.getGradeName())) {
            viewHolder.tv_book_grade.setText(mContext.getString(R.string.book_grade,
                    epaperInfo.getGradeName()));
        }else{
            String grade = EConstants.GRADE_DATA.get(epaperInfo.getGrade());
            viewHolder.tv_book_grade.setText(mContext.getString(R.string.book_grade,
                    TextUtils.isEmpty(grade) ? "无" : grade));
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView tv_bookName;
        public TextView tv_service_name;
        public android.widget.ImageView bookIcon;
        public ImageView img_item_free;
        public ImageView iv_exam;
        public TextView tv_book_grade;
        public Button btn_add_book_to_bookcase;
        public LinearLayout ll_add_book_to_bookcase;
    }

    public void setKeyWords(String keyWords) {
        if (TextUtils.isEmpty(keyWords)) {
            this.keyWords = "";
        } else {
            this.keyWords = keyWords;
        }
    }
}
